package com.ruiyangwang.ListenBook.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruiyangwang.ListenBook.util.DButil;
import com.ruiyangwang.ListenBook.viewObject.UserV0;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author syzygy
 * @date 2022/07/21 15:44
 **/
@WebServlet("/studio/album/list.json")
public class ListJsonServlet extends HttpServlet {
    private static class ResultView{
        public UserV0 currentUser;
        public List<AlbumView> albumViewList = new ArrayList<>();
    }
    private static  class AlbumView{
        public Integer aid;
        public String title;
        public  String cover;
        public String state;
        public AlbumView (int aid, String title, String cover,int state){
            this.aid = aid;
            this.title = title;
            this.cover = cover;
            switch (state){
                case 0:this.state = "已下线";break;
                case 1:this.state = "未发布";break;
                case 2:this.state = "已发布";break;
                default:this.state = "未知";break;
            }
        }
    }
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取当前登录账户
        HttpSession session = req.getSession(false);
        req.setCharacterEncoding("utf-8");
        UserV0 currentUser = null;
        if(session != null){
            currentUser = (UserV0) session.getAttribute("currentUser");
        }
        if(currentUser == null){
            throw new RuntimeException("用户未登录");
        }
        //2.执行sql
        //3.组装成需要的格式
        ResultView resultView = new ResultView();
        resultView.currentUser = currentUser;
        try(Connection c = DButil.connection()){
            String sql = "select aid,title,cover,state from albums where uid = ? order by aid desc";
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,currentUser.uid);
                try(ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        AlbumView view = new AlbumView(
                                rs.getInt("aid"),
                                rs.getString("title"),
                                rs.getString("cover"),
                                rs.getInt("state")
                        );
                        resultView.albumViewList.add(view);
                    }
                }
            }
        }

        //4.json
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultView);
        //5.相应
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);


    }
}